package chapter11;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;

/**
 * @author : DuJiabao
 * @Date : 2021/2/19 17:40
 * @Project : thinking_in_java
 * @File : null.java
 * @Desc :
 */
public class ListIteratorTest {
  @Test
  public void test() {
    List<Integer> list = new ArrayList<Integer>();
    Collections.addAll(list, 1, 2, 3, 4, 5, 6, 7, 8, 9);
    ListIterator<Integer> listIterator = list.listIterator();

    while (listIterator.hasNext()) {
      // 获取下一个值、下一个值的下标、上一个值的下标
      System.out.println("next():" + listIterator.next() + "  nextIndex():" + listIterator.nextIndex() +
              "  previousIndex():" + listIterator.previousIndex());
    }

    while (listIterator.hasPrevious()) {
      System.out.println("previous():" + listIterator.previous());
    }

    // 从坐标为3开始的迭代器
    listIterator = list.listIterator(3);
    while (listIterator.hasPrevious()) {
      System.out.println("previous():" + listIterator.previous());
    }
    while (listIterator.hasNext()) {
      // 获取下一个值、下一个值的下标、上一个值的下标
      System.out.println("next():" + listIterator.next() + "  nextIndex():" + listIterator.nextIndex() +
              "  previousIndex():" + listIterator.previousIndex());
    }
  }
}
